package com.exonline.backstage.controller.login;

import cn.hutool.core.util.IdUtil;
import com.exonline.backstage.entity.Manager;
import com.exonline.commons.utils.LogincheckUtils;
import com.exonline.commons.utils.TokenUtil;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
/**
 *登录模块c层
 * @author YUKI
 * @date 2024/11/29
 */
@WebServlet("/admin/login")
public class LoginServlet extends HttpServlet {


    ObjectMapper mapper = new ObjectMapper();
    LoginService service = new LoginService();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String method = req.getParameter("method");
        try {
            if ("login".equals(method)) {
                this.login(req, resp);
            } else if ("register".equals(method)) {
                this.register(req, resp);
            } else if ("logout".equals(method)) {
                this.logout(req, resp);
            }
        }catch (Exception e){
            throw new ServletException("登录模块出现错误,错误信息:"+e.getMessage(),e);
        }
    }

    private void logout(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        req.getSession().invalidate();
        resp.sendRedirect("https://www.baidu.com");
    }

    private void register(HttpServletRequest req, HttpServletResponse resp) throws IOException, SQLException {
        HttpSession session = req.getSession(true);
        String id = IdUtil.simpleUUID();
        String name = req.getParameter("name");
        String password = req.getParameter("password");
        System.out.println("name:" + name + "," + "password:" + password);
        Map<String, Object> map = new HashMap<>();
        if (!LogincheckUtils.checkLogin(name,password)){
            map.put("code", 201);
            map.put("msg", "注册失败,");
            map.put("data", name);
            mapper.writeValue(resp.getWriter(), map);
        }

        int count = service.register(id, name, password);
        if (count != 0) {
            map.put("code", 200);
            map.put("msg", "注册成功");
            map.put("data", id);
            mapper.writeValue(resp.getWriter(), map);
            session.setAttribute("id", id);
        }else {
            map.put("code", 201);
            map.put("msg", "注册失败,该用户名已经存在");
            map.put("data", name);
            mapper.writeValue(resp.getWriter(), map);
        }
    }

    private void login(HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException, NoSuchAlgorithmException {
        String name = req.getParameter("name");
        String password = req.getParameter("password");
        System.out.println("name:" + name + "," + "password:" + password);
        Map<String, Object> map = new HashMap<>();
        if(!LogincheckUtils.checkAdminLogin(name,password)){
            map.put("code", 201);
            map.put("msg", "登录失败,用户名z或密码格式错误");
            map.put("data", name+":"+password);
            mapper.writeValue(resp.getWriter(), map);
        }
        Manager manager = (Manager) service.login(name, password);
        
        if (manager != null) {
            map.put("code", 200);
            map.put("msg", "登录成功");
            map.put("data", manager);
////                HttpSession session = req.getSession();
////                session.setAttribute("id", manager.getId());
////                session.setAttribute("name", manager.getAname());
////                session.setAttribute("rname",manager.getRname());
//            System.out.println("111"+session);
            String token = TokenUtil.createToken(manager.getId(), manager.getAname());
            map.put("token", token);
            mapper.writeValue(resp.getWriter(), map);
        }else {
            map.put("code", 201);
            map.put("msg", "登录失败");
            map.put("data", manager);
            mapper.writeValue(resp.getWriter(), map);
        }

    }
}
